<template>
    <div class="productionStatistics">
        <el-row :gutter="20">
            <el-col class="aaa" :span="show ? rowObj.T1 : rowObj.F1" :xs="show ? rowObj.T2 : rowObj.F2">
                <workSiteLeft ref="workSiteLeft" :show="show" @showLeft="showLeft" @queryVal="queryVal" />
            </el-col>
            <el-col :span="show ? rowObj.T3 : rowObj.F3" :xs="show ? rowObj.T4 : rowObj.F4">
                <el-form :inline="true" :model="queryParams" size="small">
                    <el-button style="margin: 3px 8px 0 0;" size="mini" :icon="show ? 'el-icon-s-fold' : 'el-icon-s-unfold'"
                        circle @click="showLeft(show)"></el-button>
                    <el-form-item label="拌合楼">
                        <el-select style="width: 160px" v-model="queryParams.stationCode" clearable size="mini"
                            placeholder="请选择拌合楼">
                            <el-option v-for="item in cementListData" :key="item.id" :label="item.name" :value="item.code">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="项目名称">
                        <el-input v-model="queryParams.projectName" placeholder="项目名称" clearable></el-input>
                    </el-form-item>
                    <el-form-item label="选择时间">
                        <el-select filterable v-model="timeType" style="width: 100px; margin-right: 8px"
                            :popper-append-to-body="false">
                            <el-option label="投产以来" value="all"></el-option>
                            <el-option label="按年" value="year"></el-option>
                            <el-option label="自定义" value="other"></el-option>
                        </el-select>
                        <deatetime v-if="'other' == timeType" ref="deatetime" v-model="queryParams.timeRange">
                        </deatetime>
                        <el-date-picker style="width: 130px" v-if="'year' == timeType" v-model="queryParams.year"
                            value-format="yyyy" format="yyyy" type="year" placeholder="选择年"> </el-date-picker>
                    </el-form-item>
                    <el-form-item>
                        <el-button icon="el-icon-search" type="primary" @click="handleQuery">查询</el-button>
                    </el-form-item>
                    <div style="float: right">
                        <el-form-item>
                            <el-button type="warning" plain icon="el-icon-download" @click="onExport">导出</el-button>
                        </el-form-item>
                    </div>
                </el-form>
                <el-table :data="dataList" show-overflow-tooltip="true" border size="mini" highlight-current-row stripe>
                    <el-table-column align="center" prop="projectName" label="项目名称"></el-table-column>
                    <el-table-column align="center" prop="matchCode" label="配方编号"></el-table-column>
                    <el-table-column align="center" prop="pavingLevelLabel" label="摊铺层级"></el-table-column>
                    <el-table-column align="center" prop="batch" label="采集批次"></el-table-column>
                    <el-table-column align="center" prop="weight" label="生产重量(吨)"></el-table-column>
                </el-table>
                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getList" />
            </el-col>
        </el-row>
    </div>
</template>
  
<script>
import {
    cementList,
    getLastYear,
    pageProjectStatistics,
} from "@/api/waterStability/statistics/productionStatistics";
import deatetime from "@/components/Datetime";
import workSiteLeft from "@/views/components/workSiteLeft";
export default {
    name: 'productionStatistics', // 各项目产量统计
    components: { deatetime, workSiteLeft },

    data() {
        return {
            show: true,
            rowObj: {
                T1: 3,
                T2: 24,
                T3: 21,
                T4: 24,
                F1: 0,
                F2: 0,
                F3: 24,
                F4: 24,
            },
            timeType: 'all',
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                stationCode: undefined,
                year: undefined,
                timeRange: [undefined, undefined]
            },
            cementListData: [],
            quListData: [],
            total: 0,
            dataList: [],
            loading: false,
        };
    },
    mounted() {
        this.getLastYearApi();
        this.getCementList();


    },
    methods: {
        showLeft(show) {
            this.show = !show;
        },
        queryVal(val) {
            this.queryParams.id = val.id;
            this.handleQuery();
        },
        // 查询拌合楼下拉列表
        getCementList() {
            cementList().then((res) => {
                this.cementListData = res.data || [];
            });
        },
        /**
         * 获取最新年份数据
         */
        getLastYearApi() {
            getLastYear().then((res) => {
                if (!!res.data) {
                    this.timeType = "year";
                    this.lastYear = res.data[0];
                    this.queryParams["year"] = res.data[0];
                    this.getList();
                }
            });
        },

        getList() {
            this.loading = true;
            pageProjectStatistics(this.queryParams).then((response) => {
                this.dataList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        handleQuery() {
            this.queryParams = this.getSearchParams();
            this.queryParams.pageNum = 1;
            this.getList();
        },
        getSearchParams() {
            const params = {};
            for (const key in this.queryParams) {
                if (key === "time") {
                    const time = this.queryParams[key];
                    if (time && time.length === 2) {
                        params.startTime = time[0];
                        params.endTime = time[1];
                    }
                } else {
                    params[key] = this.queryParams[key];
                }
            }
            return params;
        },
        /** * 数据导出*/
        onExport() {
            this.$message.info('正在开发中~~~~')
            //   api
            //     .exportExcel(this.searchParams)
            //     .then((res) => {
            //       const blob = new Blob([res]);
            //       var href = window.URL.createObjectURL(blob);
            //       var downloadElement = document.createElement('a');
            //       downloadElement.href = href;
            //       downloadElement.download = decodeURIComponent('各项目产量统计.xls');
            //       document.body.appendChild(downloadElement);
            //       downloadElement.click();
            //       document.body.removeChild(downloadElement);
            //       window.URL.revokeObjectURL(href);
            //     })
            //     .catch(() => {});
        }
    }
};
</script>
  
<style scoped lang="scss">
.productionStatistics {
    padding: 16px;
}
</style>